package com.repair.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class DaoFactory {
	private static Map<String,Object> daoMap = null;
	private static DaoFactory instance = new DaoFactory();
	
	@SuppressWarnings("rawtypes")
	private DaoFactory(){
		
		daoMap = new HashMap<String,Object>();
		Properties p = null;
		InputStream is = null;
		
		is = DaoFactory.class.getClassLoader().getResourceAsStream("com/repair/config/daoConfig.properties");
		p = new Properties();
		try {
			p.load(is);
			Enumeration e = p.keys();
			String key = null;
			String value = null;
			while(e.hasMoreElements()){
				key = (String)e.nextElement();
				value = p.getProperty(key);
				Class c = Class.forName(value);
				daoMap.put(key, c.newInstance());
			}
		} 
		catch (IOException e) {
		} 
		catch (ClassNotFoundException e) {
		} 
		catch (InstantiationException e) {
		} 
		catch (IllegalAccessException e) {
		} 
		finally{
			try {
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
			}
		}
	}
	
	public static DaoFactory getInstance(){
		return instance;
	}
	
	public Object getDao(String key){
		return daoMap.get(key);
	}
}
